#include <climits>
#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    string str1, str2;
    cin >> str1 >> str2;
    vector<string> strs;
    strs.resize(n);
    for(int i=0; i<n; i++)
        cin >> strs[i];

    int prev1 = -1, prev2 = -1, len = INT_MAX;
    for(int i=0; i<n; i++)
    {
        if(strs[i] == str1)
        {
            prev1 = i;
            if(prev2 != -1)
                len = min(len, abs(prev1-prev2));
        }
        if(strs[i] == str2)
        {
            prev2 = i;
            if(prev1 != -1)
                len = min(len, abs(prev2-prev1));
        }
    }
    len = (len == INT_MAX ? -1 : len);
    cout << len << endl;
    return 0;
}
